<?php

namespace App\Services\Stats\Game\Best\Elasticsearch;

use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Core\Traits\Numeral;

class PlayerGainService extends Service
{
    use DatabaseElasticsearch, Numeral;

    /**
     * 制作统计参数
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    private function makeAggParams($request)
    {
        $datetime = $this->getParamArray($request, 'datetime');
        $topStats = $this->getParamArray($request, 'topStats');

        return [
            'body' => [
                'size' => 0,
                'aggs' => [
                    'group_stats' => [
                        'terms' => [
                            'field' => 'game_id',
                            'size' => 10000,
                        ],
                        'aggs' => [
                            'group_bucket_sort' => [
                                'bucket_sort' => [
                                    'from' => $topStats['from'],
                                    'size' => $topStats['size'],
                                    'sort' => [
                                        [
                                            'group_sort_stats' => [
                                                'order' => $topStats['stats'] == 'min' ? 'asc' : 'desc'
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'group_sort_stats' => [
                                $topStats['stats'] => [
                                    'field' => 'usd.gain_player'
                                ]
                            ],
                            'group_time' => [
                                'date_histogram' => [
                                    'field' => 'end_time_at',
                                    'time_zone' => '+08:00',
                                    'calendar_interval' => '1d',
                                    'format' => 'yyyy-MM-dd',
                                    'min_doc_count' => 0,
                                    'extended_bounds' => [
                                        'min' => $datetime[0] * 1000,
                                        'max' => $datetime[1] * 1000,
                                    ],
                                ],
                                'aggs' => [
                                    'time_stats' => [
                                        'sum' => [
                                            'field' => 'usd.gain_player'
                                        ]
                                    ],
                                ],
                            ],
                        ]
                    ]
                ]
            ]
        ];
    }

    /**
     * 获取排行数据
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function getTopList($request)
    {
        // 制作统计参数
        $params = $this->makeAggParams($request);

        // 筛选【平台】
        $this->makeWhereParamOrg($request, $params);
        // 筛选【平台】【排除】
        $this->makeWhereParamNot($request, 'plat_code_exclude', $params, 'plat_code');
        // 筛选【游戏】
        $this->makeWhereParam($request, 'game_id', $params);
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $params, 'end_time');

        // 查询数据
        $results = SettlementInfo::search($params);

        // 制作数据
        $legend = $series = $times = [];
        foreach ($results['aggregations']['group_stats']['buckets'] ?? [] as $group) {
            // 标记名称
            $legend[] = $group['key'];
            // 单组数据
            $serve = [
                'name' => $group['key'], 'data' => []
            ];
            foreach ($group['group_time']['buckets'] as $item) {
                // 数据轴
                $serve['data'][] = $item['time_stats']['value'];
                // 时间轴
                if (!count($series)) {
                    $times[] = $item['key_as_string'];
                }
            }
            $series[] = $serve;
        }

        return [
            'legend' => $legend,
            'series' => $series,
            'times' => $times,
        ];
    }
}
